﻿using Furion.InstantMessaging;
using LocalNas.Core.Const;
using Microsoft.AspNetCore.SignalR;

namespace LocalNas.Application.Hub
{
    [MapHub("/hubs/loghub")]
    public class LogHub : Hub<ILogClient>
    {
        private readonly IHubContext<LogHub, ILogClient> _logHubContext;
        public LogHub(IHubContext<LogHub, ILogClient> logHubContext)
        {
            _logHubContext = logHubContext;
        }

        public override async Task OnConnectedAsync()
        {
            var token = Context.GetHttpContext().Request.Query["access_token"];
            var claims = JWTEncryption.ReadJwtToken(token)?.Claims;
            var name = claims.FirstOrDefault(e => e.Type == ClaimConst.Name)?.Value;
            await _logHubContext.Clients.Client(Context.ConnectionId).ReceiveMessage(new Dto.SendMessageData
            {
                MessageType = Dto.MessageTypeEnum.通知,
                Message = $"{name}，欢迎回来！"
            });

        }
    }
}
